﻿#if DEBUG
namespace Test.Serializer {

    using LiteByte;
    using LiteByte.Converters;
    using Test.Entity;

    public class PetEntitySerializer : ILBSerializer<PetEntity> {

        public static byte[] Serialize(PetEntity entity) {
            var writer = new LBWriter();
            Write(writer, entity);
            return writer.ToBytes();
        }

        public static PetEntity Deserialize(byte[] bytes) {
            var reader = new LBReader(bytes);
            return Read(reader);
        }

        public static void Write(LBWriter writer, PetEntity entity) {
            if (entity == null) {
                writer.WriteBit1(true);
                return;
            }
            writer.WriteBit1(false);
            writer.WriteInt32(entity.id);
            writer.WriteVUnicode(entity.name);
            writer.WriteInt32(entity.ownerID);
            writer.WriteVUnicode(entity.ownerName);
        }



        public static PetEntity Read(LBReader reader) {
            if (reader.ReadBit1()) return null;
            var entity = new PetEntity();
            entity.id = reader.ReadInt32();
            entity.name = reader.ReadVUnicode();
            entity.ownerID = reader.ReadInt32();
            entity.ownerName = reader.ReadVUnicode();
            return entity;
        }

    }

}
#endif